package org.stormphoenix.bbsfamily.config

import org.springframework.security.core.GrantedAuthority
import org.springframework.security.core.authority.SimpleGrantedAuthority
import org.springframework.security.core.userdetails.UserDetails
import org.stormphoenix.bbsfamily.entity.UserDO

class UserDetailsDO(user: UserDO) : UserDetails {

    var user: UserDO = user

    override fun getAuthorities(): MutableCollection<out GrantedAuthority> {
        val authorities = mutableListOf<GrantedAuthority>()
        user?.let { it ->
            it.roles?.let { it ->
                for (role in it) {
                    role.authorities?.let {
                        for (authority in it) {
                            authorities.add(SimpleGrantedAuthority("ROLE_${authority.name}"))
                        }
                    }
                }
            }
        }
        return authorities
    }

    override fun isEnabled(): Boolean = user.enabled

    override fun getUsername(): String = user.username

    override fun getPassword(): String = user.password

    override fun isCredentialsNonExpired(): Boolean {
        // TODO 默认 true
        return true
    }

    override fun isAccountNonExpired(): Boolean {
        // TODO 默认true
        return true
    }

    override fun isAccountNonLocked(): Boolean {
        // TODO 默认true
        return true
    }
}